package com.xiaoye.qbproject.controller.qbzt;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xiaoye.qbproject.controller.base.BaseController;
import com.xiaoye.qbproject.domain.AjaxResult;
import com.xiaoye.qbproject.domain.DataImport;
import com.xiaoye.qbproject.page.TableDataInfo;
import com.xiaoye.qbproject.service.DataImportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/dataImport")
public class DataImportController extends BaseController {

    @Autowired
    DataImportService dataImportService;

    @GetMapping("/list")
    public TableDataInfo list(DataImport dataImport, HttpServletRequest request) {
        startPage();
        LambdaQueryWrapper<DataImport> queryWrapper = Wrappers.lambdaQuery(DataImport.class).orderByDesc(DataImport::getCreateTime);
        if (dataImport.getFileName() != null) {
            queryWrapper.like(DataImport::getFileName, dataImport.getFileName());
        }
        List<DataImport> list = dataImportService.list(queryWrapper);
        return getDataTable(list);
    }

    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable String id) {
        return AjaxResult.success(dataImportService.getById(id));
    }

    @PostMapping
    public AjaxResult add(@RequestBody DataImport dataImport) {
        dataImport.setCreateTime(new Date());
        dataImport.setUpdateTime(new Date());
        return toAjax(dataImportService.save(dataImport));
    }

    @PutMapping
    public AjaxResult edit(@RequestBody DataImport dataImport) {
        return toAjax(dataImportService.updateById(dataImport));
    }

    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        if (ids == null || ids.length == 0) {
            return AjaxResult.error("请选择要删除的数据");
        }
        dataImportService.removeBatchByIds(Arrays.asList(ids));
        return AjaxResult.success();
    }

    /**
     * 处理数据压缩包
     */
    // 导入压缩包
    @PostMapping("/parseData/{id}")
    public AjaxResult parseData(@PathVariable String id) {
        dataImportService.parseData(id);
        return AjaxResult.success();
    }

}
